package com.snails.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.snails.logger.Logger;
import com.snails.ui.R;

public class EnvDialog extends Dialog implements View.OnClickListener {
    private final Context mCtx;
    private String testUrl = "";
    private String productUrl = "";
    private OnEnvClickListener listener = null;

    private ImageView mTestIcon, mProductIcon, mDebugIcon;
    private boolean isTest = true;

    public EnvDialog(@NonNull Context context, Builder builder) {
        super(context, R.style.CommonDialog);
        this.mCtx = context;
        testUrl = builder.testUrl;
        productUrl = builder.productUrl;
        listener = builder.listener;
        isTest = builder.realUrl.equals(testUrl);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_environment);
        setUi().setOnClick().setCanceledOnTouchOutside(false);
    }

    private EnvDialog setUi() {
        ((TextView) findViewById(R.id.test_des)).setText(testUrl);
        ((TextView) findViewById(R.id.product_des)).setText(productUrl);
        mTestIcon = findViewById(R.id.test_check);
        mProductIcon = findViewById(R.id.product_check);
        setDebugUi().reset().setChecked(isTest);
        return this;
    }

    private EnvDialog setDebugUi() {
        mDebugIcon = findViewById(R.id.debug_check);
        final TextView txt = findViewById(R.id.debug_label);
        final String headerStr = mCtx.getString(R.string.a_core_dialog_title_debug);
        if (Logger.getThreadInfo()) {
            mDebugIcon.setImageResource(R.drawable.i_acore_checked);
            txt.setText(headerStr + "{开启}");
        } else {
            mDebugIcon.setImageResource(R.drawable.i_acore_unchecked);
            txt.setText(headerStr + "{关闭}");
        }
        return this;
    }

    private EnvDialog setOnClick() {
        findViewById(R.id.btn_test).setOnClickListener(this);
        findViewById(R.id.btn_product).setOnClickListener(this);
        findViewById(R.id.btn_debug).setOnClickListener(this);
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_ok).setOnClickListener(this);
        return this;
    }

    private EnvDialog reset() {
        mTestIcon.setImageResource(R.drawable.i_acore_unchecked);
        mProductIcon.setImageResource(R.drawable.i_acore_unchecked);
        return this;
    }

    private EnvDialog setChecked(boolean isTest) {
        this.isTest = isTest;
        mTestIcon.setImageResource(isTest ? R.drawable.i_acore_checked : R.drawable.i_acore_unchecked);
        mProductIcon.setImageResource(isTest ? R.drawable.i_acore_unchecked : R.drawable.i_acore_checked);
        return this;
    }

    @Override
    public void onClick(View v) {
        if (R.id.btn_test == v.getId()) {
            reset().setChecked(true);
        } else if (R.id.btn_product == v.getId()) {
            reset().setChecked(false);
        } else if (R.id.btn_debug == v.getId()) {
            Logger.setThreadInfo(!Logger.getThreadInfo());
            this.dismiss();
        } else if (R.id.btn_cancel == v.getId()) {
            this.dismiss();
            if (listener != null) {
                listener.onCancel();
            }
        } else if (R.id.btn_ok == v.getId()) {
            this.dismiss();
            if (listener != null) {
                listener.onOK(isTest);
            }
        }
    }

    // **************************************************************************
    // ===== 构造器 =====
    // **************************************************************************
    public static class Builder {
        private String testUrl = "";
        private String productUrl = "";
        private String realUrl = "";
        private OnEnvClickListener listener = null;

        public Builder() {
        }

        public Builder testUrl(String testUrl) {
            this.testUrl = testUrl;
            return this;
        }

        public Builder productUrl(String productUrl) {
            this.productUrl = productUrl;
            return this;
        }

        public Builder realUrl(String realUrl) {
            this.realUrl = realUrl;
            return this;
        }

        public Builder listener(OnEnvClickListener listener) {
            this.listener = listener;
            return this;
        }

        public EnvDialog build(Context ctx) {
            return new EnvDialog(ctx, this);
        }
    }

    // **************************************************************************
    // ===== 事件监听器 =====
    // **************************************************************************
    public interface OnEnvClickListener {
        void onCancel();

        void onOK(boolean isTest);
    }

}
